#!/bin/bash
#IModule

#########################
#   名称: IniConfig
#   用途: 提供ini配置读写功能
#   出处: https://blog.csdn.net/wanxiaoderen/article/details/82388091?utm_source=blogxgwz0
#########################

#shellcheck disable=2206
#shellcheck disable=2286
#shellcheck disable=2086
#shellcheck disable=2068
#shellcheck disable=2154
if [ -z "$_IniConfig_Loaded" ];then
    function _framework_resolve_dependencies()
    {
        true;
    }

    #result
    _IniConfig_IniOptions=()
    _IniConfig_IniSections=()
    
    function _IniConfig_ReadOptionFromFile()
    {
        local file="$1";
        local section="$2";
        local option="$3";

        if [ ! -f "$file" ];then
            touch "$file";
        fi;

        if [ "${section}" = "" ];then
            #通过如下两条命令可以解析成一个数组
            allSections=$(awk -F '[][]' '/\[.*]/{print $2}' "${file}")
            _IniConfig_IniSections=(${allSections// /})
        elif [ "${section}" != "" ] && [ "${option}" = "" ];then
            #判断section是否存在
            allSections=$(awk -F '[][]' '/\[.*]/{print $2}' "${file}")
            echo $allSections | grep ${section}
            if [ "$?" = "1" ];then
                echo "[error]:section --${section}-- not exist!"
                return 0
            fi
            #正式获取options
            #a=(获取匹配到的section之后部分|去除第一行|去除空行|去除每一行行首行尾空格|将行内空格变为@G@(后面分割时为数组时，空格会导致误拆))
            a=$(awk "/\[${section}\]/{a=1}a==1"  "${file}"|sed -e'1d' -e '/^$/d'  -e 's/[ \t]*$//g' -e 's/^[ \t]*//g' -e 's/[ ]/@G@/g' -e '/\[/,$d' )
            b=(${a})
            for i in ${b[@]};do
              #剔除非法字符，转换@G@为空格并添加到数组尾
              if [ -n "${i}" ]||[ "${i}" != "@S@" ];then
                  _IniConfig_IniOptions[${#_IniConfig_IniOptions[@]}]=${i//@G@/ }
              fi
            done
        elif [ -n "${section}" ] && [ -n "${option}" ];then
            local _IniConfig_Output;
            _IniConfig_Output="$(awk -F '=' "/\[${section}\]/{a=1}a==1" "${file}"|sed -e '1d' -e '/^$/d' -e '/^\[.*\]/,$d' -e "/^${option}.*=.*/!d" -e "s/^${option}.*= *//")";
            if [ -z "$_IniConfig_Output" ];then
                return 1;
            else
                echo "$_IniConfig_Output";
            fi;
        fi
    }
    
    function _IniConfig_WriteOptionToFile()
    {
        local file="$1";
        local section="$2"
        local option="$3"
        local value="$4"

        if [ ! -f "$file" ];then
            touch "$file";
        fi;

        #检查文件
        allSections=$(awk -F '[][]' '/\[.*]/{print $2}' "${file}")
        _IniConfig_IniSections=(${allSections// /})
        #判断是否要新建section
        sectionFlag="0"
        for temp in ${_IniConfig_IniSections[@]};do
            if [ "${temp}" = "${section}" ];then
                sectionFlag="1"
                break
            fi
        done
    
        if [ "$sectionFlag" = "0" ];then
            echo "[${section}]" >>"${file}"
        fi
        #加入或更新value
        if awk "/\[${section}\]/{a=1}a==1" "${file}"|sed -e '1d' -e '/^$/d'  -e 's/[ \t]*$//g' -e 's/^[ \t]*//g' -e '/\[/,$d'|grep "${option}.\?=">/dev/null;then
            #更新
            #找到制定section行号码
            sectionNum=$(sed -n -e "/\[${section}\]/=" "${file}")
            sed -i "${sectionNum},/^\[.*\]/s/\(${option}.\?=\).*/\1 ${value}/g" "${file}"
        else
            #新增
            sed -i "/^\[${section}\]/a\\${option} = ${value}" "${file}"
        fi
    }

    readonly _IniConfig_Loaded=true;
fi;